/**
 * 
 */
package org.textplantform.common.util;

/**
 * 用来关闭JVM的程序.一般在linux下面使用 kill pid 不要使用kill -9
 * 正常使用kill pid命令jvm退出时会运行shutdownhook线程来完成资源的扫尾工作.
 * @author alex
 *
 */
public class GracefulShutdown {
	private static volatile boolean running = true;
	private static Object lock ;
	public static void waitSigTerm(Object clz){
		lock = clz;
		Runtime.getRuntime().addShutdownHook(new Thread(){
			public void run() {
				running = false;
				synchronized (lock) {
					lock.notifyAll();
				}
				System.out.println("running hook .....");
			}
			
		});
		synchronized (lock) {
			while(running){
				try{
					lock.wait();
				}catch(Exception e){
					
				}
			}
		}
		System.out.println("exit");
	}
}
